package com.test.qrcode.manage.service.impl;

import javax.annotation.Resource;

import com.test.qrcode.manage.mapper.UserMapper;
import com.test.qrcode.manage.model.entity.User;
import com.test.qrcode.manage.service.UserService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @program qrcode
 * @Title: UserServiceImpl
 * @description:
 * @author: XueXiao
 * @create: 2019-02-27 18:01:37
 */
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
@Component("userService")
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    @Resource
    // 默认按名称(userMapper)注入,若名称匹配失败，则按照类型匹配
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Transactional(readOnly = false)   //必须设置为false,因为此处切入了日志的保存逻辑
    public Object getUser(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public User findUserById(long id) {
        return userMapper.findUserById(id);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUserById(long id) {
        userMapper.deleteUserById(id);
    }

    @Override
    public List<User> findAllUsers() {
        return userMapper.findAllUsers();
    }

    @Override
    public boolean isUserExist(User user) {
        return findUserById(user.getId()) != null;
    }

    @Override
    public void addUser(User user) {
        userMapper.saveUser(user);
    }
}
